<?php

/**
 * uniBoard Task Controller
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class app_task{
	
	// ------------------------------------------------------------------------
	
	/**
	 * Initialise Application
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Get Task ID
		$task_id = (int) $appCore -> _REQ( 't');
		
		// Load tasks from cache
		$tasks = (array) class_cache::loadCache( 'sheduled_tasks');
		
		// Cache not loaded?
		if ( class_cache::$load_error)
		{
			// Select Tasks
			$appCore -> db -> query( 'SELECT task_id, task_file, task_days, task_hours, task_minutes, task_next_run
			FROM ' . DB_PREFIX . 'tasks
			WHERE task_active = \'1\'');
			
			// Build taks array from result
			while ( $task_result = $appCore -> db -> fetch_array())
			{
				$tasks[] = $task_result;
			}
			
			// Free Result
			$appCore -> db -> freeResult();
			
			// Save Cache
			class_cache::saveCache( 'sheduled_tasks', $tasks);
		}
		
		// Reiterate tasks
		$tasks_lenght = count( $tasks);
		
		// Find our task
		if ( $tasks_lenght > 0)
		{
			for ( $t = 0; $t < $tasks_lenght; $t ++)
			{
				// Run Time All-right?
				if ( $tasks[$t][0] == $task_id && $tasks[$t][5] <= $appCore -> time)
				{
					// Include Task file?
					if ( strpos( $tasks[$t][1], '/') === false && strpos( $tasks[$t][1], '\\') == false && file_exists( TASKS_PATH . $tasks[$t][1]))
					{
						// Include file
						require_once( TASKS_PATH . $tasks[$t][1]);
												
						// Is there a point in updating Run Date?
						if ( $tasks[$t][2] + $tasks[$t][3] + $tasks[$t][4] > 0)
						{
							// Update next run
							$tasks[$t][5] = $appCore -> time + ( $tasks[$t][2] * 86400) + ( $tasks[$t][3] * 3600) + ( $tasks[$t][4] * 60);
						
							// Save Cache
							class_cache::saveCache( 'sheduled_tasks', $tasks);
						
							// Yep, gonna update DB
							$appCore -> db -> update( array( 'task_next_run' => $tasks[$t][5]), 'tasks', 'task_id = \'' . $task_id . '\'');
						}
												
						// Class defined?
						if ( class_exists( 'task'))
						{
							// Run Task
							new task( $appCore);
						}
					}
					
					// Break iteration
					break;
				}
			}
		}
		
		// Send empty GIF as our only output
		$this -> sendOutput();
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Sends empty gif to client
	 *
	 */
	
	function sendOutput(){
		
		// Send gif image header
		header('Content-type: image/gif');
		
		// Echo blank gif
		echo base64_decode( 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
		
	}
	
}